package cn.com.surker.system.controller;

import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.SysRole;
import cn.com.surker.system.service.RoleService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
* <p>
    * 角色表 前端控制器
    * </p>
*
* @author lucky
* @since 2021-05-21
*/

@RestController
@RequestMapping("/console/role")
public class SysRoleController {

    /**实业实现**/
    @Resource
    private RoleService roleService;

    /**
     *  分页查询角色用户/菜单列表
     * @param rid 角色ID
     * @param name 员工名称
     * @param type 类型(1:角色用户,2:角色菜单)
     * @param menuType 菜单类型(0:手机菜单,1:后台菜单)
     * @param offset 开始数
     * @param limit 每页大小
     **/
    @RequestMapping(value = "/query/relation", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto queryRelationRole(Long rid, String name, Integer type, Integer offset, Integer limit, Integer menuType){
        try {
            if (StringUtils.isNull(offset))
                offset = 0;

            if (StringUtils.isNull(limit))
                limit = 15;

            if (type == 1) {
                Map<String, Object> map = roleService.queryRoleUser(rid, name, offset, limit);
                /**分页角色用户对象**/
                return RestResultDto.success(map);
            } else {
                /**分页菜单用户对象**/
                Map<String, Object> map = roleService.queryRoleMenu(rid, name, menuType, offset, limit);
                return RestResultDto.success(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,加载数据失败");
        }
    }

    /**
     * 添加角色菜单/角色用户关联
     * @param rid 角色ID
     * @param oid 对象ID(菜单/用户)
     * @param type 类型(1:角色用户,2:角色菜单)
     * @return
     */
    @RequestMapping(value = "/update/relation/{rid}", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto updateRoleRelation(@PathVariable Long rid, Long oid, Integer type, Integer status) {
        try {
            if (type == 1)
                if (status == 0)
                /**角色用户对象**/
                    return RestResultDto.complete(roleService.saveRoleUser(oid, rid));
                else if (status == 1)
                    return RestResultDto.complete(roleService.deleteRoleUser(oid, rid));
                else
                    return RestResultDto.fail("参数类型错误");
            else
                if (status == 0)
                /**角色菜单对象**/
                    return RestResultDto.complete(roleService.saveRoleMenu(oid, rid));
                else if (status == 1)
                    return RestResultDto.complete(roleService.deleteRoleMenu(oid, rid));
                else
                    return RestResultDto.fail("参数类型错误");
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

    /**
     *  分页查询列表
     * @param dto
     * @param pageIndex 每页开始数
     * @param limit 每页大小
     **/
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto findAll(SysRole dto, Integer offset, Integer pageIndex, Integer limit){
        try {
            if (StringUtils.isNull(pageIndex))
                pageIndex = 1;

            if (StringUtils.isNull(limit))
                limit = 15;

            /**分页对象**/
            Page<SysRole> page = new Page<>(offset, limit);
            IPage iPage = roleService.findAllByPage(dto, offset, limit);
            return RestResultDto.success(iPage);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,加载数据失败");
        }
    }

    /**
     *  新增数据
     * @param dto
     **/
    @RequestMapping(value = "/save", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto save(SysRole dto){
        try {
            return RestResultDto.complete(roleService.saveInit(dto));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

    /**
     * 更新数据状态
     * @param id 角色ID
     * @param status 状态(0:启用,1;禁用)
     * @return
     */
    @RequestMapping(value = "/update/status/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto update(@PathVariable Long id, Integer status) {
        try {
            return RestResultDto.complete(roleService.updateStatus(id, status));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

    /**
     * 更新数据
     * @param dto
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public RestResultDto update(@PathVariable Long id, SysRole dto) {
        try {
            return RestResultDto.complete(roleService.update(dto));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

}



